//
//  KStatusDynamicCell.m
//  Keping
//
//  Created by Joanlove on 2017/12/17.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "KStatusDynamicCell.h"
#import <Masonry/Masonry.h>

@interface KStatusDynamicCell()

@property(nonatomic,weak) IBOutlet UIImageView* avatarView;
/** 发布动态的人 */
@property(nonatomic,weak) IBOutlet YYLabel* nameLabel;
/** 动态时间 */
@property(nonatomic,weak) IBOutlet YYLabel* timeLabel;
/**  动态内容 */
@property(nonatomic,weak) IBOutlet YYLabel* contentLabel;
/** 动态内容的高度 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentNSHeigth;
/** 动态图片的View */
@property (weak, nonatomic) IBOutlet UIView *dynamicPics;
/** 动态图片的View的高度 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *dynamicPicNCHeigth;
/** 喜欢动态的人数 */
@property(nonatomic,weak) IBOutlet YYLabel* likesLabel;
/** 评论动态的人数 */
@property(nonatomic,weak) IBOutlet YYLabel* commentsLabel;
/** 评论动态的按钮 */
@property(nonatomic,weak) IBOutlet UIButton* commentButton;
/** 点赞动态的按钮 */
@property(nonatomic,weak) IBOutlet UIButton* likeButton;

@end

@implementation KStatusDynamicCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)layoutSubviews {
    WEAKSELF
    //头像
    [self.avatarView setImageWithURL:[NSURL URLWithString:self.layout.status.img?:@""] placeholder:[UIImage imageNamed:KImagePlaceholder] options:kNilOptions progress:nil transform:nil completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        if (!image) {
            weakSelf.avatarView.image = [[UIImage imageNamed:KImagePlaceholder] imageByResizeToSize:CGSizeMake(KStatusCellAvatarWH, KStatusCellAvatarWH)];
        }else{
            weakSelf.avatarView.image = [image imageByResizeToSize:weakSelf.avatarView.size contentMode:UIViewContentModeScaleAspectFill];
        }
    }];
    
    //名称
    self.nameLabel.textLayout = self.layout.nameLayout;
    //时间
    self.timeLabel.textLayout = self.layout.timeLayout;
    //内容
    self.contentLabel.textLayout = self.layout.textLayout;
    self.contentNSHeigth.constant = self.layout.textHeight;
    //图片
    if (self.layout.imagesHeight > 0.0) {
        [self.dynamicPics removeAllSubviews];
        CGSize picSize =  self.layout.picSize;
        NSArray *pics = self.layout.status.imageUrls;
        int picsCount = (int)pics.count;
        __block UIImageView *blockImg = nil;
        for (int i = 0; i < picsCount; i++) {
            
            UIImageView *imageView = [[UIImageView alloc] init];
            NSString* url = pics[i];
            @weakify(imageView);
            [imageView setImageWithURL:[NSURL URLWithString:[KGetImageURL stringByAppendingString:url]?:@""] placeholder:[UIImage imageNamed:@"user_placeholder_icon"] options:kNilOptions completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
                weak_imageView.image = [image imageByResizeToSize:picSize contentMode:UIViewContentModeScaleAspectFill];
            }];
            [self.dynamicPics addSubview:imageView];
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                if (i % 3 == 0 && i == 0) {
                    make.left.equalTo(self.dynamicPics.mas_left).offset(10);
                    make.top.equalTo(self.dynamicPics.mas_top);
                } else if (i % 3 == 2){
                    make.left.equalTo(self.dynamicPics.mas_left);
                    make.top.equalTo(blockImg.mas_bottom).offset(10);
                } else {
                    make.left.equalTo(blockImg.mas_right).offset(10);
                    make.top.equalTo(blockImg.mas_top);
                }
                make.height.mas_equalTo(picSize.height);
                make.width.mas_equalTo(picSize.width);
                blockImg = imageView;
            }];
        }
    } else {
        self.dynamicPicNCHeigth.constant = 0.0;
    }
    
    //喜欢动态的人数
    self.likesLabel.textLayout = self.layout.likeTextLayout;
    self.commentsLabel.textLayout = self.layout.commentTextLayout;
}

- (void)setLayout:(KStatusLayout *)layout {
    if (!layout)
        return;
    _layout = layout;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
